package com.zxy.ider.ehome.widget;


import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import com.zxy.ider.ehome.R;


public class MainUpView extends FrameLayout {

	private static final String TAG = "MainUpView";
	private static final float DEFUALT_SCALE = 1.0f;

	private BaseEffectBridge mEffectBridge;

	public MainUpView(Context context) {
		super(context, null, 0);
		if (context != null && context instanceof Activity) {
			attach2Window((Activity) context);
		}
		init(context, null);
	}

	/**
	 * 鎵嬪姩娣诲姞锛屼笉鍦╔ML娣诲姞鐨勮瘽.
	 */
	private void attach2Window(Activity activity) {
		ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
		ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		rootView.addView(this, layoutParams);
		rootView.setClipChildren(false);
		rootView.setClipToPadding(false);
	}

	public MainUpView(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		init(context, attrs);
	}

	public MainUpView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		setWillNotDraw(false);
		initEffectBridge();
		// 鍒濆锟�??.
		if (attrs != null) {
			TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.MainUpView);// 鑾峰彇閰嶇疆灞烇拷??
			Drawable drawableUpRect = tArray.getDrawable(R.styleable.MainUpView_upImageRes); // 椤跺眰鍥剧墖.
			Drawable drawableShadow = tArray.getDrawable(R.styleable.MainUpView_shadowImageRes); // 闃村奖鍥剧墖.
			setUpRectDrawable(drawableUpRect);
			setShadowDrawable(drawableShadow);
			tArray.recycle();
		}
	}

	private void initEffectBridge() {
		BaseEffectBridge baseEffectBridge = new OpenEffectBridge();
		baseEffectBridge.onInitBridge(this);
		baseEffectBridge.setMainUpView(this);
		setEffectBridge(baseEffectBridge);
	}

	/**
	 * 璁剧疆锟�??涓婂眰鐨勫浘鐗囪祫婧怚D.
	 */
	public void setUpRectResource(int resId) {
		if (mEffectBridge != null)
			mEffectBridge.setUpRectResource(resId);
	}

	/**
	 * 璁剧疆锟�??涓婂眰鐨勮竟锟�??.
	 */
	public void setUpRectDrawable(Drawable upRectDrawable) {
		if (mEffectBridge != null)
			mEffectBridge.setUpRectDrawable(upRectDrawable);
	}

	/**
	 * 鑾峰彇锟�??涓婂眰鍥剧墖.
	 */
	public Drawable getUpRectDrawable() {
		if (mEffectBridge != null) {
			return mEffectBridge.getUpRectDrawable();
		}
		return null;
	}

	/**
	 * 鏍规嵁鍥剧墖杈规 鑷 濉啓 鐩稿樊鐨勮竟锟�??. <br>
	 * 姣斿 res/drawble/white_light_10.9.png鐨勫浘鐗囷紝杈硅窛灏卞樊寰堝.
	 * 
	 * @param size
	 *            璐熸暟杈规鍑忓皬锛屾鏁板弽锟�??(闃村奖杈规锟�??鏍风殑).
	 */
	public void setDrawUpRectPadding(int size) {
		setDrawUpRectPadding(new Rect(size, size, size, size));
	}

	/**
	 * 鏍规嵁鍥剧墖杈规 鑷 濉啓 鐩稿樊鐨勮竟锟�??. <br>
	 * 姣斿 res/drawble/white_light_10.9.png鐨勫浘鐗囷紝杈硅窛灏卞樊寰堝.
	 */
	public void setDrawUpRectPadding(Rect rect) {
		if (mEffectBridge != null) {
			mEffectBridge.setDrawUpRectPadding(rect);
			invalidate();
		}
	}

	/**
	 * 鑾峰彇锟�??涓婂眰鍥剧墖 闂磋窛鐭╁舰(Rect).
	 */
	public Rect getDrawUpRect() {
		if (mEffectBridge != null) {
			return mEffectBridge.getDrawUpRect();
		}
		return null;
	}

	/**
	 * 璁剧疆闃村奖鍥剧墖璧勬簮ID.
	 */
	public void setShadowResource(int resId) {
		if (mEffectBridge != null) {
			this.mEffectBridge.setShadowResource(resId);
			invalidate();
		}
	}

	/**
	 * 褰撳浘鐗囪竟妗嗕笉鑷甫闃村奖鐨勮瘽锛屽彲浠ヨ嚜琛岃缃槾褰卞浘锟�??. 璁剧疆闃村奖.
	 */
	public void setShadowDrawable(Drawable shadowDrawable) {
		if (mEffectBridge != null) {
			this.mEffectBridge.setShadowDrawable(shadowDrawable);
			invalidate();
		}
	}

	/**
	 * 鑾峰彇闃村奖鍥剧墖.
	 */
	public Drawable getShadowDrawable() {
		if (mEffectBridge != null) {
			return this.mEffectBridge.getShadowDrawable();
		}
		return null;
	}

	/**
	 * 鏍规嵁闃村奖鍥剧墖杈规 鑷 濉啓 鐩稿樊鐨勮竟锟�??. <br>
	 * 姣斿 res/drawble/white_shadow.9.png鐨勫浘鐗囷紝杈硅窛灏卞樊寰堝.
	 */
	public void setDrawShadowPadding(int size) {
		setDrawShadowRectPadding(new Rect(size, size, size, size));
	}

	/**
	 * 鏍规嵁闃村奖鍥剧墖杈规 鑷 濉啓 鐩稿樊鐨勮竟锟�??. <br>
	 * 姣斿 res/drawble/white_shadow.9.png鐨勫浘鐗囷紝杈硅窛灏卞樊寰堝.
	 */
	public void setDrawShadowRectPadding(Rect rect) {
		if (mEffectBridge != null) {
			mEffectBridge.setDrawShadowRectPadding(rect);
			invalidate();
		}
	}

	/**
	 * 鑾峰彇闃村奖鍥剧墖杈硅窛.
	 */
	public Rect getDrawShadowRect() {
		if (mEffectBridge != null) {
			return mEffectBridge.getDrawShadowRect();
		}
		return null;
	}

	/**
	 * 鐒︾偣鎺т欢澶勭悊.
	 * 
	 * @param newView
	 *            鏂扮劍鐐筕iew
	 * @param oldView
	 *            鑰佺劍鐐筕iew
	 * @param scale
	 *            鏀惧ぇ鐨勬瘮锟�??, 鍙互璁剧疆锟�??1.2f, 1.1f鎴栵拷?锟藉叾锟�??
	 */
	public void setFocusView(View newView, View oldView, float scale) {
		setFocusView(newView, scale);
		setUnFocusView(oldView);
	}

	/**
	 * FocusView澶勭悊.
	 * 
	 * @param view
	 * @param scale
	 *            鏀惧ぇ姣斾緥
	 */
	public void setFocusView(View view, float scale) {
		setFocusView(view, scale, scale);
	}

	/**
	 * 
	 * @param view
	 * @param scaleX
	 *            X鏀惧ぇ姣斾緥
	 * @param scaleY
	 *            Y鏀惧ぇ姣斾緥
	 */
	public void setFocusView(View view, float scaleX, float scaleY) {
		if (this.mEffectBridge != null)
			this.mEffectBridge.onFocusView(view, scaleX, scaleY);
	}

	/**
	 * 璁剧疆鏃犵劍鐐瑰瓙鎺т欢杩樺師.(榛樿锟�??1.0F)
	 * 
	 * @param view
	 *            鑰佺劍鐐筕iew.
	 */
	public void setUnFocusView(View view) {
		setUnFocusView(view, DEFUALT_SCALE, DEFUALT_SCALE);
	}

	/**
	 * 鑰佺劍鐐筕IEW澶勭悊.
	 * 
	 * @param view
	 * @param scaleX
	 *            X缂╂斁姣斾緥
	 * @param scaleY
	 *            Y缂╂斁姣斾緥
	 */
	public void setUnFocusView(View view, float scaleX, float scaleY) {
		if (this.mEffectBridge != null)
			this.mEffectBridge.onOldFocusView(view, scaleX, scaleY);
	}

	/**
	 * 璁剧疆EffectBridge.
	 */
	public void setEffectBridge(BaseEffectBridge effectBridge) {
		this.mEffectBridge = effectBridge;
		if (this.mEffectBridge != null) {
			this.mEffectBridge.onInitBridge(this);
			this.mEffectBridge.setMainUpView(this);
			invalidate();
		}
	}

	public BaseEffectBridge getEffectBridge() {
		return this.mEffectBridge;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (this.mEffectBridge != null) {
			if (this.mEffectBridge.onDrawMainUpView(canvas)) {
				return;
			}
		}
		super.onDraw(canvas);
	}

}
